<?php

include dirname(__FILE__).'/../../../DBConnector/idiorm/config2.php';

class NewXML {

    private $_sales_notes = array();
    private $_description = array();
    private $_deliveries = array();
    private $_sales_notes_products = array();
    private $_description_products = array();
    public $currency = 'RUR';
    private $_output;

    public function __construct() {
        $this->get_description();

        $this->gen_xml();
        $this->send_output();
    }

    private function get_description() {


        $model = ORM::for_table('store_product_xml_data')->select_many('id', 'type', 'value')->where_in('type', array('sales_notes', 'description'))->find_many();

        foreach ($model as $value) {

            if ($value->type == 'sales_notes') {
                $this->_sales_notes[$value->id] = $value->value;
                continue;
            }

            if ($value->type == 'description') {
                $this->_description[$value->id] = $value->value;
            }
        }

        $model = ORM::for_table('store_product_xml_data_relation')->select_many('xml_data_id', 'product_code');

        $value = array();
        foreach ($model as $value) {

            if (array_key_exists($value->xml_data_id, $this->_sales_notes)) {
                $this->_sales_notes_products[$value->product_code] = $value->xml_data_id;
                continue;
            }


            if (array_change_key_case($value->xml_data_id, $this->_description)) {
                $this->_description_products[$value->product_code] = $value->xml_data_id;
                continue;
            }
        }
    }

    function gen_xml() {

        global $COST_OF_DELIVERY_1;
        $this->_deliveries = $COST_OF_DELIVERY_1;
        $date = date('Y-m-d H:i', time());

        $xml = new SimpleXMLElement('<!DOCTYPE yml_catalog SYSTEM "shops.dtd"><yml_catalog date="' . $date . '" ></yml_catalog>');


//        $yml = $xml->addChild('yml_catalog>', '');
//        $yml->addAttribute('date', date('Y-m-d H:i', time()));


        $shop = $xml->addChild('shop', '');

        $shop->addChild('name', SITE_NAME);
        $shop->addChild('company', SITE_NAME);
        $shop->addChild('url', HOST_NAME);
        $currencies = $shop->addChild('currencies');
        $currency = $currencies->addChild('currency');
        $currency->addAttribute('id', 'RUR');
        $currency->addAttribute('rate', 1);
        $currency->addAttribute('plus', 1);
        $categories = $shop->addChild('categories', '');


        $cats_model = ORM::for_table('pages')->select_many('id', 'title', 'parents', 'templ')->where_in('templ', array('category', 'sub_category'))->find_many();

        foreach ($cats_model as $cat_model) {

            $parentId = ( $cat_model->templ == 'sub_category' ) ?
                    ' parentId="' . array_pop($this->_getIdsArr($cat_model->parents)) . '"' : '';

            $cat = $categories->addChild('category', $cat_model->title);
            $cat->addAttribute('id', $cat_model->id);

            if (!empty($parentId))
                $cat->addAttribute('parentId', $parentId);
        }


        $offers = $shop->addChild('offers', '');
        $offers_model = ORM::for_table('store_products_cache')->where('pd', 50)->where_gt('price_retail', 0)->find_many();

        foreach ($offers_model as $offer_model) {
            

            $offer = $offers->addChild('offer', '');
            $offer->addAttribute('id', $offer_model->product_code);

            $available = ($offer_model->pp == 1) ? 'true' : 'false';
            $offer->addAttribute('available', $available);
            $offer->addChild('url', HOST_NAME . $offer_model->url);
            $offer->addChild('price', round($offer_model->price_retail, 0));
            $offer->addChild('currencyId', $this->currency);

            $cat_id = array_pop($this->_getIdsArr($offer_model->parents));
            $offer->addChild('categoryId', $cat_id);
            $offer->addChild('picture', HOST_NAME . '/inc/images/products/' . $offer_model->product_code . '.jpg');

       
                   if (isset($this->_deliveries[$offer_model->volume]))
                    $offer->addChild('local_delivery_cost', $this->_deliveries[$offer_model->volume]);
      



            $prefix = $this->replaceIllegalChars($offer_model->prefix);
            $vendor = $this->replaceIllegalChars($offer_model->brand);
            $name = $this->replaceIllegalChars($offer_model->name);
            $full_name = $offer_model->market_name ? $this->replaceIllegalChars($offer_model->market_name) : $prefix . " " . $vendor . " " . $name;
            $offer->addChild('name', $full_name);


            $description = $this->replaceIllegalChars(isset($this->_description_products[$offer_model->product_code]) ? ( isset($this->_description[$this->_description_products[$offer_model->product_code]]) ? $this->_description[$this->_description_products[$offer_model->product_code]] : '' ) : '' );

            if (!empty($description))
                $offer->addChild('description', $description);


            $offer->addChild('manufacturer_warranty', 'true');

            if (!empty($offer_model->country_of_origin))
                $offer->addChild('country_of_origin', $offer_model->country_of_origin);
        }



        $this->_output = $xml->asXML();
    }

    protected function send_output() {

        $data = iconv("windows-1251", "utf-8", $this->_output);
        file_put_contents('/var/www/raybt/data/www/raybt.ru/inc/ym2_price.xml', $data);
    }

    private function replaceIllegalChars($str) {
        $healthy = array('"', '&', '>', '<', "'");
        $yummy = array('&quot;', '&amp;', '&gt;', '&lt;', '&apos;');
        return str_replace($healthy, $yummy, $str);
    }

    private function _getIdsArr($str, $d = '#') {
        $str = preg_replace(array('@^' . $d . '@', '@' . $d . '$@'), '', $str);
        return ($str == '') ? array() : explode($d . $d, $str);
    }

    public function __destruct() {
        unset($this);
    }

}
new NewXML();
die();






